package designpattern.OpenClosedPrinciple.old;

public class Alert {
    private AlertRule rule;
    private Notification notification;

    public Alert(AlertRule rule, Notification notification) {
        this.rule = rule;
        this.notification = notification;
    }

    /**
     * 上面这段代码非常简单，业务逻辑主要集中在 check() 函数中。
     * 当接口的 TPS 超过某个预先设置的最大值时，
     * 以及当接口请求出错数大于某个最大允许值时，就会触发告警，
     * 通知接口的相关负责人或者团队。
     *
     *
     * @param api
     * @param requestCount
     * @param errorCount
     * @param durationOfSeconds
     */
    public void check(String api, long requestCount, long errorCount, long durationOfSeconds) {
        long tps = requestCount / durationOfSeconds;
        if (tps > rule.getMatchedRule(api).getMaxTps()) {
            notification.notify(NotificationEmergencyLevel.URGENCY, "...");
        }
        if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
            notification.notify(NotificationEmergencyLevel.SEVERE, "...");
        }
    }
}